package com.lanxiao.cloud.common.core.bus.listeners;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import com.lanxiao.cloud.common.core.bus.event.DomainEvent;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import org.apache.commons.lang3.concurrent.BasicThreadFactory.Builder;

/**
 * @author ：qiaoliang
 * @date ：2021-09-09
 */
public abstract class AbstractGuavaDomainEventPublisher  implements DomainEventPublisher {

  private EventBus syncBus = new EventBus(this.identify());
  private EventBus asyncBus = new AsyncEventBus(this.identify(), new ScheduledThreadPoolExecutor(1, (new Builder()).daemon(true).build()));

  public AbstractGuavaDomainEventPublisher() {
  }

  @Override
  public void register(Object listener) {
    this.syncBus.register(listener);
    this.asyncBus.register(listener);
  }

  @Override
  public void publish(DomainEvent event) {
    this.syncBus.post(event);
  }

  @Override
  public void asyncPublish(DomainEvent event) {
    this.asyncBus.post(event);
  }

}
